<template>
  <BaseLayout title="字典管理" @query="init" @reset="reset">
    <template #query>
      <el-form :inline="true" :model="queryForm">
        <el-form-item label="字典名称">
          <el-input clearable v-model="queryForm.dictTypeName" placeholder="请输入字典名称"/>
        </el-form-item>
        <el-form-item label="字典编号">
          <el-input clearable v-model="queryForm.dictTypeCode" placeholder="请输入字典编号"/>
        </el-form-item>
      </el-form>
    </template>
    <template #button>
      <el-button type="primary" @click="addDict">新增</el-button>
    </template>
    <template #table>
      <el-table v-loading="loading" :data="data" height="100%" class="w-full">
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column prop="dictTypeName" label="字典类型名称" align="left" header-align="left"></el-table-column>
        <el-table-column prop="dictTypeCode" label="字典类型编号" align="left" header-align="left"></el-table-column>
        <el-table-column prop="enabled" label="是否启用" align="center" header-align="center" width="100">
          <template #default="scope">
            <BaseTag :value="scope.row.enabled" :dict="enableOptions"></BaseTag>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" align="center" header-align="center"
                         width="180"></el-table-column>
        <el-table-column prop="realName" label="创建用户" align="center" header-align="center"
                         width="120"></el-table-column>
        <el-table-column label="操作" width="240" align="center" header-align="center">
          <template #default="scope">
            <el-button link type="primary" @click="editDict(scope.$index, scope.row)">修改</el-button>
            <el-button link type="warning" @click="clearCache(scope.$index, scope.row)">清除缓存</el-button>
            <el-button link type="danger" @click="delDict(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <AddOrUpdateDict @load="init" ref="dictRef"></AddOrUpdateDict>
    </template>
    <template #page>
      <BasePage @load="init" v-model:current="queryForm.current" v-model:size="queryForm.size"
                :total="queryForm.total"></BasePage>
    </template>
  </BaseLayout>
</template>

<script setup lang="ts" name="dict">
import AddOrUpdateDict from './AddOrUpdateDict.vue'
import {nextTick, onMounted, reactive, ref} from "vue";
import {
  clearCacheByDictTypeCode,
  deleteDictTypeByDictTypeId,
  listDictTypePage,
  selectDictTypeByDictTypeId
} from "/@/api/system/DictApi";
import {DictDataModel, DictQueryModel, DictTypeModel} from "/@/api/system/model/DictModel";
import {messageBox} from "/@/utils/BjxUtils";
import {ElMessage} from "element-plus";
import {useDictStore} from "/@/store/modules/dict";

const enableOptions = ref<DictDataModel[]>([])
const data = ref<DictTypeModel[]>([])
const dictRef = ref()
const loading = ref(false)
const queryForm = reactive<DictQueryModel>({
  dictTypeName: '',
  dictTypeCode: '',
  size: 10,
  current: 1,
  total: 0
})
const init = async () => {
  loading.value = true
  let res = await listDictTypePage({...queryForm}, () => {
    loading.value = false
  })
  data.value = res.records
  queryForm.total = res.total
}
/**
 * 新增字典
 */
const addDict = () => {
  dictRef.value.init("新增字典")
}
/**
 * 修改字典
 */
const editDict = async (index: number, row: DictTypeModel) => {
  loading.value = true
  let res = await selectDictTypeByDictTypeId(row.dictTypeId)
  dictRef.value.init("修改字典", res)
  loading.value = false
}
/**
 * 删除字典
 */
const delDict = async (index: number, row: DictTypeModel) => {
  messageBox("您确定要删除这个字典类型吗?", async () => {
    loading.value = true
    await deleteDictTypeByDictTypeId(row.dictTypeId, () => {
      loading.value = false
    })
    init()
  })

}
/**
 * 重置
 */
const reset = () => {
  queryForm.dictTypeCode = ''
  queryForm.dictTypeName = ''
  queryForm.current = 1
  init()
}
/**
 * 清除缓存
 */
const clearCache = async (index: number, row: DictTypeModel) => {
  loading.value = true
  await clearCacheByDictTypeCode(row.dictTypeCode)
  loading.value = false
}
onMounted(async () => {
  let dictStore = useDictStore();
  enableOptions.value = await dictStore.getDict('enable_dict')
  nextTick(() => {
    init()
  })

})
</script>

<style scoped>

</style>